package com.cvmars.jianyou.module.fragment;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.content.ContextCompat;
import android.support.v4.view.ViewPager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.cvmars.jianyou.R;
import com.cvmars.jianyou.module.activity.FindAddWeiboActivity;
import com.cvmars.jianyou.module.activity.FindCreateTopicActivity;
import com.cvmars.jianyou.module.activity.JiNengActivity;
import com.cvmars.jianyou.module.base.BaseActivity;
import com.cvmars.jianyou.module.listener.PermissionListener;
import com.cvmars.jianyou.utils.DensityUtils;
import com.cvmars.jianyou.widget.FindTextSizeChangTitleView;
import com.luck.picture.lib.PictureSelector;
import com.luck.picture.lib.config.PictureConfig;
import com.luck.picture.lib.config.PictureMimeType;

import net.lucode.hackware.magicindicator.MagicIndicator;
import net.lucode.hackware.magicindicator.ViewPagerHelper;
import net.lucode.hackware.magicindicator.buildins.UIUtil;
import net.lucode.hackware.magicindicator.buildins.commonnavigator.CommonNavigator;
import net.lucode.hackware.magicindicator.buildins.commonnavigator.abs.CommonNavigatorAdapter;
import net.lucode.hackware.magicindicator.buildins.commonnavigator.abs.IPagerIndicator;
import net.lucode.hackware.magicindicator.buildins.commonnavigator.abs.IPagerTitleView;
import net.lucode.hackware.magicindicator.buildins.commonnavigator.indicators.LinePagerIndicator;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import butterknife.Unbinder;

/**
 * A simple {@link Fragment} subclass.
 * Activities that contain this fragment must implement the
 * create an instance of this fragment.
 */
public class MessageFragment extends Fragment {


    MagicIndicator tabLayoutTask;
    ViewPager viewPager;

    String[] tab = {"话题", "最新", "关注", "技能"};
    @BindView(R.id.txt_find_publish)
    ImageView txtFindPublish;
    Unbinder unbinder;
    @BindView(R.id.iv_jineng)
    ImageView ivJineng;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment

        View inflate = inflater.inflate(R.layout.fragment_message, null);
        tabLayoutTask = inflate.findViewById(R.id.tab_task);
        viewPager = inflate.findViewById(R.id.vp_task);
        viewPager.setOffscreenPageLimit(4);
        viewPager.setAdapter(new FragmentPagerAdapter(getChildFragmentManager()) {
            @Override
            public Fragment getItem(int position) {
                TaskFragment taskFragment = new TaskFragment();

                if (position == 0) {

                    return new TopicFragment();
                }
                if (position == 1) {
                    Bundle bundle = new Bundle();
                    bundle.putBoolean("isHot", true);
                    taskFragment.setArguments(bundle);
                }

                if (position == 3) {

                    return new ZhiYeFragment();
                }
                return taskFragment;
            }

            @Override
            public int getCount() {
                return tab.length;
            }

            @Nullable
            @Override
            public CharSequence getPageTitle(int position) {
                return tab[position];
            }
        });
//        tabLayoutTask.setupWithViewPager(viewPager);

        CommonNavigator commonNavigator = new CommonNavigator(getContext());
        commonNavigator.setAdapter(new CommonNavigatorAdapter() {

            @Override
            public int getCount() {
                return tab.length;
            }

            @Override
            public IPagerTitleView getTitleView(Context context, final int index) {
                FindTextSizeChangTitleView simplePagerTitleView = new FindTextSizeChangTitleView(context);
                simplePagerTitleView.setTextSize(17);
                simplePagerTitleView.setNormalColor(ContextCompat.getColor(getContext(), R.color.color333));
                simplePagerTitleView.setSelectedColor(ContextCompat.getColor(getContext(), R.color.color333));
                simplePagerTitleView.setText(tab[index]);
                simplePagerTitleView.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        viewPager.setCurrentItem(index);

                    }
                });
                return simplePagerTitleView;
            }

            @Override
            public IPagerIndicator getIndicator(Context context) {
//                LinePagerIndicator indicator = new LinePagerIndicator(context);
//                indicator.setBackgroundResource(R.drawable.shape_dot);
//                indicator.setColors();

                LinePagerIndicator indicator = new LinePagerIndicator(context);
//                float navigatorHeight = context.getResources().getDimension(R.dimen.common_navigator_height);
                float borderWidth = UIUtil.dip2px(context, 3);

                indicator.setMode(LinePagerIndicator.MODE_EXACTLY);
//                float lineHeight = navigatorHeight - 2 * borderWidth;
//                indicator.setLineHeight(lineHeight);
                indicator.setLineHeight(DensityUtils.dp2px(getContext(), 5));
                indicator.setLineWidth(DensityUtils.dp2px(getContext(), 8));
                indicator.setRoundRadius(borderWidth);
                indicator.setColors(ContextCompat.getColor(getContext(), R.color.color333));
                return indicator;
            }
        });

        tabLayoutTask.setNavigator(commonNavigator);

        ViewPagerHelper.bind(tabLayoutTask, viewPager);

        unbinder = ButterKnife.bind(this, inflate);

        txtFindPublish.setOnLongClickListener(new View.OnLongClickListener() {
            @Override
            public boolean onLongClick(View view) {

                getActivity().startActivity(new Intent(getActivity(), FindAddWeiboActivity.class));
                return true;
            }
        });
        return inflate;
    }


    @Override
    public void onDestroyView() {
        super.onDestroyView();
        unbinder.unbind();
    }

    @OnClick({R.id.txt_find_publish, R.id.iv_jineng})
    public void onViewClicked(View view) {

        switch (view.getId()) {

            case R.id.txt_find_publish:
                selectMultPic();
                break;

            case R.id.iv_jineng:

                getActivity().startActivity(new Intent(getActivity(), JiNengActivity.class));

                break;

        }

    }


    //选择多张图片
    public void selectMultPic() {


//        startActivity(new Intent(getActivity(), FindCreateTopicActivity.class));

        ((BaseActivity) getActivity()).checkMyPermission(new PermissionListener(PermissionListener.PHOTO) {
            @Override
            public void onPermissionClick() {

                PictureSelector.create(getActivity())
                        .openGallery(PictureMimeType.ofImage())//全部.PictureMimeType.ofAll()、图片.ofImage()、视频.ofVideo()
                        .theme(R.style.picture_default_style)
                        .selectionMode(PictureConfig.MULTIPLE)
                        .previewImage(true)
                        .isCamera(false)
//                .previewVideo(true)
                        .enableCrop(false)// 是否裁剪 true or false
                        .compress(true)
                        .maxSelectNum(9)
                        .compressMaxKB(400)
                        .compressMode(PictureConfig.LUBAN_COMPRESS_MODE)
                        .glideOverride(800, 800)// 是否压缩 true or false
                        .withAspectRatio(1, 1)
                        .rotateEnabled(true)
                        .scaleEnabled(true)
                        .forResult(PictureConfig.CHOOSE_REQUEST);
            }
        });

    }


}
